package com.mswork.mes.service.impl;

import java.util.List;
import com.mswork.common.utils.DateUtils;
import com.mswork.mes.domain.vo.ProjectVo;
import com.mswork.mes.mapper.CompanyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;

import com.mswork.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.mswork.mes.domain.ProjectCargo;
import com.mswork.mes.mapper.ProjectMapper;
import com.mswork.mes.domain.Project;
import com.mswork.mes.service.IProjectService;

/**
 * 项目管理Service业务层处理
 * 
 * @author manshu
 * @date 2025-03-03
 */
@Service
public class ProjectServiceImpl implements IProjectService
{
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private CompanyMapper companyMapper;

    /**
     * 查询项目管理
     * 
     * @param projectId 项目管理主键
     * @return 项目管理
     */
    @Override
    public Project selectProjectByProjectId(Integer projectId)
    {
        return projectMapper.selectProjectByProjectId(projectId);
    }

    /**
     * 查询项目管理列表
     * 
     * @param project 项目管理
     * @return 项目管理
     */
    @Override
    public List<Project> selectProjectList(Project project)
    {
        List<Project> projects = projectMapper.selectProjectList(project);
        return projects;
    }
    @Override
    public List<ProjectVo> selectProjectListV2(Project project)
    {
        List<ProjectVo> projects = projectMapper.selectProjectListV2(project);
        if(projects != null && projects.size() > 0){
            List<ProjectCargo> cargos = projectMapper.selectProjectCargoListV2(projects.stream().map(ProjectVo::getProjectId).toArray(Integer[]::new));
            Map<Integer, List<ProjectCargo>> map = cargos.stream().collect(Collectors.groupingBy(ProjectCargo::getProjectId));
            projects.forEach(vo -> {
                vo.setProjectCargoList(map.get(vo.getProjectId()));
            });
        }
        return projects;
    }



    /**
     * 新增项目管理
     * 
     * @param project 项目管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProject(Project project)
    {
        project.setCreateTime(DateUtils.getNowDate());
        int rows = projectMapper.insertProject(project);
        insertProjectCargo(project);
        return rows;
    }

    /**
     * 修改项目管理
     * 
     * @param project 项目管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProject(Project project)
    {
        //修改时,原计算框架是先删后插,这里逻辑需要修改
        project.setUpdateTime(DateUtils.getNowDate());
        projectMapper.deleteProjectCargoByProjectId(project.getProjectId());
        //projectMapper.deleteProjectCargoByProjectId(project.getProjectId());
        insertProjectCargo(project);
        return projectMapper.updateProject(project);
    }

    /**
     * 批量删除项目管理
     * 
     * @param projectIds 需要删除的项目管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProjectByProjectIds(Integer[] projectIds)
    {
        projectMapper.deleteProjectCargoByProjectIds(projectIds);
        return projectMapper.deleteProjectByProjectIds(projectIds);
    }

    /**
     * 删除项目管理信息
     * 
     * @param projectId 项目管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProjectByProjectId(Integer projectId)
    {
        projectMapper.deleteProjectCargoByProjectId(projectId);
        return projectMapper.deleteProjectByProjectId(projectId);
    }

    /**
     * 新增货物信息
     * 
     * @param project 项目管理对象
     */
    public void insertProjectCargo(Project project)
    {
        List<ProjectCargo> projectCargoList = project.getProjectCargoList();
        Integer projectId = project.getProjectId();
        if (StringUtils.isNotNull(projectCargoList))
        {
            List<ProjectCargo> list = new ArrayList<ProjectCargo>();
            for (ProjectCargo projectCargo : projectCargoList)
            {
                projectCargo.setProjectId(projectId);
                projectCargo.initCargoNum();
                list.add(projectCargo);
            }
            if (list.size() > 0)
            {
                projectMapper.batchProjectCargo(list);
            }
        }
    }

    /**
     * 查询项目关联
     *
     * @param project 项目管理
     * @return 项目管理集合
     */
    public List<ProjectVo> selectProjectAll(Project project){
        List<ProjectVo> vos = projectMapper.selectProjectAll(project);
        /*List<ProjectCargo> cargos = projectMapper.selectProjectCargoListV2(vos.stream().map(ProjectVo::getProjectId).toArray(Integer[]::new));
        Map<Integer, List<ProjectCargo>> map = cargos.stream().collect(Collectors.groupingBy(ProjectCargo::getProjectId));
        vos.forEach(vo -> {
            vo.setProjectCargoList(map.get(vo.getProjectId()));
        });*/

        return vos;
    }

    /**
     * 查询项目关联
     *
     * @param project 项目管理
     * @return 项目管理集合
     */
    public ProjectVo selectProjectDetails(Integer projectId){
        ProjectVo project = projectMapper.selectProjectDetails(projectId);
        project.setProjectCargoList(projectMapper.selectProjectCargoList(project.getProjectId()));
        return project;
    }
}
